#
# CI.py - Command Interpreter
#

import L1
import commands

class player:
    def __init__(self, host="localhost", team="rukota", WMI=None):
        self.world = WMI        # this is how we will know about the world
        self.todo = []          # things to do, in form (command string, arguments)
                                # ex: todo = [("go to", (10,10))]
        
        self.player = L1.player(host=host, team=team, simpleWorld=self.world.world, \
            eqCallback = self.translateCommands)

        # dictionary of commands CI can understand, usually defined in commands.py
        # complex commands can have their own classes, threads, etc.
        self.commands = {
            "idle":commands.idle,            # sit around, do nothing
            "go to ball":commands.goToBall,  # find ball and charge at it
            "go to":commands.goTo            # loc=(x,y) - go to location
        }
    
    def translateCommands(self):
        """This function translates the todo list into L1 commands.
        It is called when L1 runs out of things to do.
        """
        if self.todo:
            # print "Doing: " + self.todo[0][0]
            apply(self.commands[self.todo[0][0]], [self.world, self.player] + self.todo[0][1:])
            
        if self.player.queue:
            self.player.send()
            
    def clearCommand(self):
        self.todo = []
